//
//  ActivityDetailController.m
//  RainbowFM
//
//  Created by chenli on 14/12/10.
//  Copyright (c) 2014年 RB. All rights reserved.
//

#import "ActivityDetailController.h"
#import "UtilsCell.h"
#import "ActivityModel.h"
#import <MediaPlayer/MediaPlayer.h>

@interface ActivityDetailController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,strong) ActivityDetailItem *detailItem;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIImageView *topImageView;
@property (weak, nonatomic) IBOutlet UIButton *applyButton;
@property (weak, nonatomic) IBOutlet UIButton *playButton;

@end


@implementation ActivityDetailController


-(void)fetchData{
    [SVProgressHUD show];
    [[ActivityModel sharedModel] fetchActivityDetailWithID:_actId result:^(BOOL success, ActivityDetailItem *item,NSString *msg) {
        if (success) {
            [SVProgressHUD dismiss];
            _detailItem = item;
            [self initViewData];
        }else{
            [SVProgressHUD showErrorWithStatus:msg];
        }
        
    }];
}

-(void)initViewData{
    self.titleLabel.text = _detailItem.actName;
    if ([_detailItem.signupFlag integerValue] == 4) {
        _applyButton.hidden = YES;
    }
    
    if ([_detailItem.bannerVideo isEqualToString:@""] || _detailItem.bannerVideo == nil) {
        self.playButton.hidden = YES;
    }else{
        self.playButton.hidden = NO;
    }
    
    [self.topImageView sd_setImageWithURL:[NSURL URLWithString:_detailItem.bannerPic]];
    
    [self.tableView reloadData];

}


-(IBAction)playAction:(id)sender{
    if (![_detailItem.bannerVideo isEqualToString:@""]) {
        NSURL *movieUrl = [NSURL URLWithString:_detailItem.bannerVideo];
        MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:movieUrl];
        player.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
    
        [self presentMoviePlayerViewControllerAnimated:player];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self fetchData];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ActivityDetailCell"];
    NSString *startTime = _detailItem.startTime;
    NSString *endTime = _detailItem.endTime;
    
    cell.timeLabel.text = [NSString stringWithFormat:@"报名时间: %@ 至 %@",[[startTime componentsSeparatedByString:@" "] firstObject],[[endTime componentsSeparatedByString:@" "] firstObject]];
    cell.contentLabel.text = _detailItem.actRule;
    return cell;
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ActivityDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ActivityDetailCell"];
    NSString *startTime = _detailItem.startTime;
    
    NSString *endTime = _detailItem.endTime;
    
    cell.timeLabel.text = [NSString stringWithFormat:@"报名时间: %@ 至 %@",[[startTime componentsSeparatedByString:@" "] firstObject],[[endTime componentsSeparatedByString:@" "] firstObject]];
    cell.contentLabel.text = _detailItem.actRule;
    [cell layoutIfNeeded];
    
    CGFloat height = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
    return height+60;
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 140;
    
}


#pragma mark - Navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    [[segue destinationViewController] setActId:_actId];
}


@end
